﻿using System.Diagnostics;
using System.Timers;
using System.Collections.Generic;
using System;
using System.Windows;
using System.Collections.ObjectModel;

namespace TaskManagerMVVM.DataAccess
{
    public class ProcessRetriever
    {

        private Process[] processes = null;
        public List<Process> processesList = null;


        public ProcessRetriever()
        {
            if (processesList == null)
            {
                processesList = new List<Process>();
            }
            Timer timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(timer_elapsed);
            timer.Enabled = true;
            timer.Start();

          
           
        }

        private void timer_elapsed(object sender, ElapsedEventArgs args)
        {
            RetrieveProcesses();
        }

        public void RetrieveProcesses()
        {
            processesList.Clear();
            //System.Threading.Thread.Sleep(100);

            processes = Process.GetProcesses();
            foreach (Process p in processes)
            {
                Console.WriteLine(p);
                processesList.Add(p);
            }

        }

        public List<Process> GetProcesses()
        {
            //System.Threading.Thread.Sleep(2000);      
            return processesList;

        }

        public void CloseProcess(Process name)
        {
            // processes = Process.GetProcessesByName(name);
            foreach (Process p in processes)
            {
                if (p == name)
                {
                    p.Kill();
                }
            }
        }


    }
}
